<?php
class PostsController extends AppController {
	var $name = 'Posts';
	var $helpers = array('Tinymce');
	//var $uses = array('User','Group','Post','Comment','Category','Comment');
	/**
	 * Xem cac tin theo thanh pho
	 * @Author: Nguyen Xuan Thach
	 * @Date: 22/04/11
	 * @Params: city_id (id thanh pho muon xem), category_id (id category muon xem)
	 *     		2 cai bang null thi xem tat ca
	 */
	function beforeFilter(){
		$this->Auth->allow('index', 'view');
		parent::beforeFilter();
		if (substr($this->params['action'], 0, 6) == 'admin_')
			$this->layout = "admin";
		else $this->layout = "lam";
	}
	function index($type_id = null, $category_id = null, $city_id = null) {
		$this->set('title_for_layout', 'Trang rao vặt');		
		$this->Post->recursive = 1;		
		// Lọc tin rao
		$conditions = null;
		$current_city = 0;		
		if ($city_id!= null && $city_id !=0) { 
			$conditions['Post.city_id'] = $city_id; 
			$current_city = $city_id; 
		}
		$current_category = 0;
		if ($category_id!= null && $category_id !=0){
			$conditions['Post.category_id'] = $category_id;
			$current_category = $category_id;
		}
		$current_type = 0;
		if ($type_id!= null && $type_id !=0){
			$conditions['Post.type_id'] = $type_id;
			$current_type = $type_id;
		}
		
		$this->paginate = array(
			'Post' => array(
				'conditions' => $conditions,				
				'order' => array(
					'Post.created' => 'desc'
				)
			)
		);				
		$cities['0'] = 'Toàn quốc';
		$categories['0'] = 'Tất cả';
		$types['0'] = 'Tất cả';
		$cities = array_merge($cities,$this->Post->City->find('list'));
		$categories = array_merge($categories,$this->Post->Category->find('list'));
		$types = array_merge($types,$this->Post->Type->find('list'));

		$this->data['Post']['category_id'] = $current_category;
		$this->data['Post']['city_id'] = $current_city;
		$this->data['Post']['type_id'] = $current_type;
		
		$this->set(compact('cities', 'categories', 'types'));
		$this->set('posts', $this->paginate());
	}

	function view($id = null) {
		$this->set('title_for_layout', 'Trang rao vặt');
		if (!$id) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}			
		
		$post = $this->Post->read(null, $id);		
		$this->set('title_for_layout', $post['Post']['title']); 
		$users = $this->Post->User->find('list');
		$this->set(compact('post', 'users'));	
			
		$this->data = $this->Post->read(null, $id);
		$this->data['Post']['count']++;
		$this->Post->save($this->data);		
		$this->Post->deleteAll(array('user_id' => 0, 'category_id' => 0), true, false);		
	}

	function add() {
		$this->set('title_for_layout', 'Thêm tin rao vặt');
		if (!empty($this->data)) {
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('Đăng tin thành công', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Có lỗi khi đăng tin. Xin vui lòng thử lại.', true));
			}
		}
		$users = $this->Post->User->find('list');
		$categories = $this->Post->Category->find('list');
		$cities = $this->Post->City->find('list');
		$types = $this->Post->Type->find('list');
		
		$this->set(compact('users', 'categories','cities', 'types'));
	}

	function edit($id = null) {
		$this->set('title_for_layout', 'Sửa tin rao vặt');
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('Sửa tin thành công', true));
				$this->redirect(array('action' => 'view', $id));
			} else {
				$this->Session->setFlash(__('Có lỗi khi sửa tin. Xin vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		$users = $this->Post->User->find('list');
		$categories = $this->Post->Category->find('list');
		$cities = $this->Post->City->find('list');
		$types = $this->Post->Type->find('list');
		$this->set(compact('users', 'categories','cities', 'types'));
	}
	function admin_index() {
		$this->Post->recursive = 0;
		$this->set('posts', $this->paginate());
	}
	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		$users = $this->Post->User->find('list');
		$categories = $this->Post->Category->find('list');
		$this->set(compact('users', 'categories'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for post', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Post->delete($id)) {
			$this->Session->setFlash(__('Post deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Post was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	

	/** 
	 * Lay tong so tin dang co trong web. Duoc dung trong element "thongkechung"
	 *
	 * @Author:	Le Tran Lam
	 * @Date: 	18/04/11
	 * @Email: 	tranlam181@gmail.com
	 */
	 
	 function getSumPost(){
		return $this->Post->find('count');
	 }
	 
	 /** 
	 * Hàm search, dùng trong default.ctp
	 * @return: các post thỏa $id của category (nằm trong $this->data)
	 *
	 * @Author:	Le Tran Lam
	 * @Date: 	18/04/11
	 * @Email: 	tranlam181@gmail.com
	 */
	 
	 function Search(){
		$this->set('title_for_layout', 'Tìm kiếm');
		$id = $this->data['Post']['select'];
		$text = $this->data['Post']['fkeyword'];
		$this->Post->recursive = 1; // Thạch thay đổi 
		$kq = null;
		if($id == 'all'){
			//$kq = $this->Post->find('all', array('conditions' => array('Post.title LIKE' => "%{$text}%")));	
			$conditions = array('Post.title LIKE' => "%{$text}%");
			$this->paginate = array(
				'Post' => array(
					'conditions' => $conditions,
					'order' => array(
						'Post.created' => 'desc'
					),
					'limit' => 30
				)
			);
		}else {
			//$kq = $this->Post->find('all', array('conditions' => array('Post.category_id' => $id, 'Post.title LIKE' => "%{$text}%")));
			$conditions = array('Post.title LIKE' => "%{$text}%", 'Post.category_id' => $id);
			$this->paginate = array(
				'Post' => array(
					'conditions' => $conditions,
					'order' => array(
						'Post.created' => 'desc'
					),
					'limit' => 30
				)
			);
		}
		$this->set('kq', $this->paginate());
	 }
	 
	function search_advance(){
		$this->set('title_for_layout', 'Tìm nâng cao');	
	}
	 
	function search_advance_result(){
		$this->set('title_for_layout', 'Kết quả tìm kiếm');
		if(!empty($this->data)){
			$kq = null;
			$conditions = null;	
			if($this->data['Post']['ftype'] == 'tieude'){				
				$conditions['Post.title LIKE'] = "%{$this->data['Post']['fkeyword']}%";
			}else{				
				$conditions['Post.content LIKE'] = "%{$this->data['Post']['fkeyword']}%";
			}
			//
			if($this->data['Post']['fadstype'] != 'all'){							
				$conditions['Post.type_id'] = $this->data['Post']['fadstype'];
			}
			//
			if($this->data['Post']['fcat'] != 'all'){
				$conditions['Post.category_id'] = $this->data['Post']['fcat'];
			}
			//
			if($this->data['Post']['fcity'] != 'all'){			
				$conditions['Post.city_id'] = $this->data['Post']['fcity'];
			}
			//
			if($this->data['Post']['fadsday'] != 'all'){																	
				$t = $this->data['Post']['fadsday'];
				$t = "-{$t} day";
				$today = date('Y-m-j');
				$new_date = strtotime ($t, strtotime($today)) ;	// cach day "fadsday" ngay
				$new_date = date('Y-m-j', $new_date); // la ngay nay
				
				$conditions['Post.created >='] = $new_date;
			}
			//
			$sapxep = "";
			if($this->data['Post']['forderby'] == 'tieude'){$sapxep = 'Post.title';}
			else if($this->data['Post']['forderby'] == 'solanxem'){$sapxep = 'Post.count';}
			else if($this->data['Post']['forderby'] == 'danhmuc'){$sapxep = 'Category.name';}
			else if($this->data['Post']['forderby'] == 'ngaydang'){$sapxep = 'Post.created';}
			
			//print_r($conditions);
			$this->paginate = array(
				'Post' => array(
					'conditions' => $conditions,
					'order' => array(
						$sapxep => 'desc'
					),
					'limit' => $this->data['Post']['fcount']
				),				
			);
			$this->set('kq', $this->paginate());	
			//$this->redirect('search');
		}
	}
	
	/** 
	 * Hàm trả về danh sach 10 tin rao mới nhất và được xem nhiều nhất
	 *
	 * @Author:	Nguyen Tan Viet
	 * @Date: 	4/6/2011
	 * @Email: 	viethoavang@gmail.com
	 */
	function newest_posts(){
		$posts = $this->Post->find('all', array(
			'limits' => 10,
			'order' => 'Post.created DESC',
		));
		return $posts;
	}
	
	function most_view() {
		$posts = $this->Post->find('all', array(
			'limit' => 10,
			'order' => 'Post.count DESC',
		));
		return $posts;
	}
	
}
?>